package im.yisme.view.activities;


import im.yisme.R;
import im.yisme.controler.service.CtrlServiceProvider;
import im.yisme.controler.service.RecordCtrlService;
import im.yisme.view.service.ViewService;
import im.yisme.view.service.ViewServiceProvider;
import im.yisme.view.ui.RecordView;
import im.yisme.view.ui.component.OnClickListener;
import im.yisme.view.ui.component.YButton;
import im.yisme.view.ui.util.ButtonEventListener;
import im.yisme.view.ui.util.UIparams;
import im.yisme.vo.MusicVO;
import im.yisme.vo.RawActionVO;
import android.app.Activity;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.PointF;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.MotionEvent;

/**
 * 录制乐谱用例Activity
 * 接收玩家输入，向Model传递玩家输入，控制时刻，控制动画呈现，控制组件，控制音乐
 * @author Max
 *
 */
public class RecordActivity extends Activity{

	private RecordCtrlService recordCtrl;
	private ViewService viewCtrl;

	//时刻控制
	private long startMoment,pauseMoment;
	public boolean pause;
	
	//UI
	private RecordView view;
	private YButton pauseButton,quitButton,resumeButton,restartButton;
	private ButtonEventListener buttonEventListener;
	
	
	//音乐
	private MediaPlayer player;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		view=new RecordView(this, this);
		setContentView(view);
		
		initControler();
		initEventListener();
		initUIComponent();
	    initMusicPlayer();
	    
		
		
		
	}

	

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		//先交给ButtonEventListener做检测
		
		
		if(buttonEventListener.onButtonEvent(event)) return false;
		
		
		if(pause) return false;
		
		int moment = (int) (System.currentTimeMillis() - startMoment);

		RawActionVO rawAction;
		switch (event.getAction()) {
		case MotionEvent.ACTION_POINTER_DOWN:
			Log.i("RecordActivity", "pointer_down");
			rawAction = new RawActionVO(RawActionVO.DOWN, moment);
			rawAction.x1 = event.getX(1);
			rawAction.y1 = event.getY(1);
			Log.i("RecordActivity", rawAction.x1+":"+rawAction.y1);
			recordCtrl.input(rawAction);
			break;
			
		case MotionEvent.ACTION_POINTER_UP:
			Log.i("RecordActivity", "pointer_up");
			rawAction = new RawActionVO(RawActionVO.UP, moment);
			rawAction.x1 = event.getX(0);
			rawAction.y1 = event.getY(0);
			Log.i("RecordActivity", rawAction.x1+":"+rawAction.y1);
			recordCtrl.input(rawAction);
			break;
			
		case MotionEvent.ACTION_DOWN:
			Log.i("RecordActivity", "down");
			rawAction = new RawActionVO(RawActionVO.DOWN, moment);
			rawAction.x1 = event.getX(0);
			rawAction.y1 = event.getY(0);
			Log.i("RecordActivity", rawAction.x1+":"+rawAction.y1);
			recordCtrl.input(rawAction);
			break;
			
		case MotionEvent.ACTION_MOVE:
			Log.i("RecordActivity", "move");
			rawAction = new RawActionVO(RawActionVO.MOVE, moment);
			rawAction.x1 = event.getX(0);
			rawAction.y1 = event.getY(0);
			if(event.getPointerCount()>1){
				rawAction.x2 = event.getX(1);
				rawAction.y2 = event.getY(1);
			}
			Log.i("RecordActivity", rawAction.x1+":"+rawAction.y1);
			Log.i("RecordActivity", rawAction.x2+":"+rawAction.y2);
			
			
			recordCtrl.input(rawAction);
			
			//view.addNewMovePoint(new PointF(event.getX(), event.getY()));
			  
			break;
			
		case MotionEvent.ACTION_UP:
			Log.i("RecordActivity", "up");
			rawAction = new RawActionVO(RawActionVO.UP, moment);
			rawAction.x1 = event.getX(0);
			rawAction.y1 = event.getY(0);
			Log.i("RecordActivity", rawAction.x1+":"+rawAction.y1);
			
			recordCtrl.input(rawAction);
			
			view.setTrackBegin();
			break;
			
		case MotionEvent.ACTION_POINTER_2_DOWN:
			Log.i("RecordActivity", "down_2");
			rawAction = new RawActionVO(RawActionVO.DOWN, moment);
			rawAction.x1 = event.getX(1);
			rawAction.y1 = event.getY(1);
			Log.i("RecordActivity", rawAction.x1+":"+rawAction.y1);
			recordCtrl.input(rawAction);
			break;
			
		case MotionEvent.ACTION_POINTER_2_UP:
			Log.i("RecordActivity", "up_2");
			rawAction = new RawActionVO(RawActionVO.UP, moment);
			rawAction.x1 = event.getX(1);
			rawAction.y1 = event.getY(1);
			Log.i("RecordActivity", rawAction.x1+":"+rawAction.y1);
			recordCtrl.input(rawAction);
			break;
			
		default:
			Log.i("RecordActivity", "yes");
			return false;
		}

		return true;
	}

	
	
	// ----------------------------------------------------------------------
	// activity生命周期相关
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		pause();
	}

	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		super.onRestart();
		resume();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		viewCtrl.updateTopActivity(this);
		
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
	}

	//私有方法

	private void initControler() {
		recordCtrl = CtrlServiceProvider.getRecordCtrlService();
		recordCtrl.setView(view);
		recordCtrl.reset();
		viewCtrl = ViewServiceProvider.getViewService();
	}
	private void initUIComponent() {
		// TODO Auto-generated method stub
		
		
		pauseButton=new YButton();
		pauseButton.setBounds(750, 0, 50, 50);
		pauseButton.setBgp_pressed(BitmapFactory.decodeResource(getResources(), R.drawable.game_pause));
		pauseButton.setBgp_released(BitmapFactory.decodeResource(getResources(), R.drawable.game_pause));
		pauseButton.setOnClickListener(new OnClickListener() {
			
			public void OnClick() {
				// TODO Auto-generated method stub
				pause();
				quitButton.setVisible(true);
				resumeButton.setVisible(true);
				restartButton.setVisible(true);
				pauseButton.setVisible(false);
			}
		});
		buttonEventListener.registerButton(pauseButton);
		view.addYButton(pauseButton);
		
		quitButton=new YButton();
		quitButton.setBounds(500, 215, 50, 50);
		quitButton.setBgp_pressed(BitmapFactory.decodeResource(getResources(), R.drawable.game_back));
		quitButton.setBgp_released(BitmapFactory.decodeResource(getResources(), R.drawable.game_back));
		quitButton.setOnClickListener(new OnClickListener() {
			
			public void OnClick() {
				// TODO Auto-generated method stub
				//quit();
				jumpToResult();
			}
		});
		quitButton.setVisible(false);
		buttonEventListener.registerButton(quitButton);
		view.addYButton(quitButton);
		
		resumeButton=new YButton();
		resumeButton.setBounds(250, 215, 50, 50);
		resumeButton.setBgp_pressed(BitmapFactory.decodeResource(getResources(), R.drawable.game_continue));
		resumeButton.setBgp_released(BitmapFactory.decodeResource(getResources(), R.drawable.game_continue));
		resumeButton.setOnClickListener(new OnClickListener() {
			
			public void OnClick() {
				// TODO Auto-generated method stub
				resume();
				resumeButton.setVisible(false);
				quitButton.setVisible(false);
				restartButton.setVisible(false);
				pauseButton.setVisible(true);
			}
		});
		resumeButton.setVisible(false);
		buttonEventListener.registerButton(resumeButton);
		view.addYButton(resumeButton);
		
		restartButton = new YButton();
		restartButton.setBounds(375, 215, 50, 50);
		restartButton.setBgp_pressed(BitmapFactory.decodeResource(getResources(), R.drawable.game_restart));
		restartButton.setBgp_released(BitmapFactory.decodeResource(getResources(), R.drawable.game_restart));
		restartButton.setOnClickListener(new OnClickListener() {
			
			public void OnClick() {
				// TODO Auto-generated method stub
				Intent intent = new Intent(RecordActivity.this, RecordActivity.class);
				Intent intent2 = getIntent();
				intent.putExtra("lyricsVO", intent2.getSerializableExtra("lyricsVO"));
				intent.putExtra("musicVO", intent2.getSerializableExtra("musicVO"));
				viewCtrl.jumpTo(intent);
				
				resumeButton.setVisible(false);
				quitButton.setVisible(false);
				restartButton.setVisible(false);
				pauseButton.setVisible(true);
			}
		});
		restartButton.setVisible(false);
		buttonEventListener.registerButton(restartButton);
		view.addYButton(restartButton);
	}
	private void initEventListener(){
		buttonEventListener=new ButtonEventListener();
	}
	private void initMusicPlayer(){
		Intent intent = getIntent();		
		MusicVO musicVO = (MusicVO) intent.getSerializableExtra("musicVO");
		
		recordCtrl.setRecordingMusic(musicVO.getId());
		
		player =MediaPlayer.create(getApplicationContext(), Uri.parse(musicVO.getFilePath()));
		player.setLooping(false);
        player.setOnCompletionListener(new OnCompletionListener() {
			
			public void onCompletion(MediaPlayer mp) {
				// TODO Auto-generated method stub
				finishRecord();
			}
		});
	}
    private int inWhichPillar(float x,float y){
    	int d;
    	
    	for(int i=0;i<UIparams.NUM_OF_PILLAR;i++){
    		d=(int) Math.hypot(x-UIparams.center[i][0], y-UIparams.center[i][1]);
    		if(d<UIparams.TOUCH_RANGE) return i;
    	}
    	
    	return -1;
    }
    private void pause(){
    	pauseMoment=System.currentTimeMillis();
    	view.pause();
    	player.pause();
    	pause=true;
    }
    private void resume(){
    	startMoment=startMoment+(System.currentTimeMillis()-pauseMoment);
    	view.resume();
    	player.seekTo((int)(System.currentTimeMillis()-startMoment));
    	player.start();
    	pause=false;
    }
    private void quit(){
    	
    	recordCtrl.quit();
    	
    	//跳转到音乐列表界面
    }
    public void start(){
    	startMoment=System.currentTimeMillis();
    	player.start();
    }
    private void finishRecord(){
    	jumpToResult();
    }
    private void jumpToResult(){
    	Intent intent = new Intent(this, RecordOverActivity.class);
    	
    	Intent intent1 = getIntent();		
		MusicVO musicVO = (MusicVO) intent1.getSerializableExtra("musicVO");
		
		intent.putExtra("musicVO", musicVO);
		
    	viewCtrl.jumpTo(intent);
    }
	
}
